﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QuanLyDiem.models;
using Library.UI.Grid;
using Library.UI.Form;
using QuanLyDiem.Controllers;
namespace QuanLyDiem.QLDaoTao
{
    public partial class frmChuyenKhoa : DevExpress.XtraEditors.XtraForm
    {
        public frmChuyenKhoa()
        {
            InitializeComponent();
       
        }
        public static List<DanhSachLop> danhsachlop=  new List<DanhSachLop>();
        public static int id_lop1s;
        public static int id_lop2s;
        private void frmChuyenKhoa_Load(object sender, EventArgs e)
        {
            Context db = new Context();
            cmbKhoa1.DataSource = db.Khoas.OrderBy(a => a.ID_khoa).ToList();
            cmbKhoa1.DisplayMember = "Ten_khoa";
            cmbKhoa1.ValueMember = "ID_khoa";
            cmbKhoa2.DataSource = db.Khoas.OrderBy(a => a.ID_khoa).ToList();
            cmbKhoa2.DisplayMember = "Ten_khoa";
            cmbKhoa2.ValueMember = "ID_khoa";
            C_SinhVien.LoadDataSource(rlueTenSinhVienFrom, db);
            C_SinhVien.LoadDataSource1(rlueMaSinhVienFrom, db);
            C_SinhVien.LoadDataSource(rlueTenSinhVienTo, db);
            C_SinhVien.LoadDataSource1(rlueMaSinhVienTo, db);


        }

        private void cmbKhoa1_SelectedValueChanged(object sender, EventArgs e)
        {
            
            Context db = new Context();
          
            int b = 0;
            if (!int.TryParse(cmbKhoa1.SelectedValue.ToString(), out b)) return;
            var lops = db.Lops.Where(a => a.ID_khoa == int.Parse(cmbKhoa1.SelectedValue.ToString()));
            
            cmbLop1.DataSource = lops.ToList();
            cmbLop1.DisplayMember = "Ten_lop";
            cmbLop1.ValueMember = "ID_lop";
            
        }

        private void cmbKhoa2_SelectedValueChanged(object sender, EventArgs e)
        {
            Context db = new Context();

            int b = 0;
            if (!int.TryParse(cmbKhoa2.SelectedValue.ToString(), out b)) return;
            if (cmbKhoa1.Text == cmbKhoa2.Text)
            {
                var lops = db.Lops.Where(a => a.ID_khoa == int.Parse(cmbKhoa2.SelectedValue.ToString()) && a.ID_lop != int.Parse(cmbLop1.SelectedValue.ToString()));
                cmbLop2.DataSource = lops.ToList();
                cmbLop2.DisplayMember = "Ten_lop";
                cmbLop2.ValueMember = "ID_lop";
            }
            else
            {
                var lops = db.Lops.Where(a => a.ID_khoa == int.Parse(cmbKhoa2.SelectedValue.ToString()) );
                cmbLop2.DataSource = lops.ToList();
                cmbLop2.DisplayMember = "Ten_lop";
                cmbLop2.ValueMember = "ID_lop";
 
            }
        }

        private void cmbLop1_SelectedValueChanged(object sender, EventArgs e)
        {
            Context db = new Context();
            int b = 0;
            if (!int.TryParse(cmbLop1.SelectedValue.ToString(), out b)) return;
            var abc = db.DanhSachLops.Where(a => a.ID_lop == int.Parse(cmbLop1.SelectedValue.ToString()));
            danhSachLopBindingSource.DataSource = abc.ToList();
            if (cmbLop2.Items.Count > 0 && cmbKhoa1.Text == cmbKhoa2.Text)
            {
                var lops = db.Lops.Where(a => a.ID_khoa == int.Parse(cmbKhoa2.SelectedValue.ToString()) && a.ID_lop != int.Parse(cmbLop1.SelectedValue.ToString()));
                cmbLop2.DataSource = lops.ToList();
                cmbLop2.DisplayMember = "Ten_lop";
                cmbLop2.ValueMember = "ID_lop";
            }
        }

        private void cmbLop2_SelectedValueChanged(object sender, EventArgs e)
        {
            //Context db = new Context();
            //var abc = db.DanhSachLops.Where(a => a.ID_lop == int.Parse(cmbLop2.SelectedValue.ToString()));
            //danhSachLopBindingSource1.DataSource = abc.ToList();
        }

        private void btnFrom1_Click(object sender, EventArgs e)
        {
            if (grvForm.RowCount == 0)
            {
                MessageBox.Show("Không có dữ liệu");
            }
            else
            {
                Context db = new Context();
                var row = grvForm.GetFocusedRow() as DanhSachLop;
                var dslop = new DanhSachLop
                {
                    ID_sv=row.ID_sv,
                    ID_lop=row.ID_lop
                };

                danhSachLopBindingSource1.Add(dslop);
                danhSachLopBindingSource.RemoveCurrent();
            }
        }

        private void btnTo1_Click(object sender, EventArgs e)
        {
            if (grvTo.RowCount == 0)
            {
                MessageBox.Show("Không có dữ liệu");
            }
            else
            {
                Context db = new Context();
                var row = grvTo.GetFocusedRow() as DanhSachLop;
                var dslop = new DanhSachLop
                {
                    ID_sv = row.ID_sv,
                    ID_lop = row.ID_lop
                };

                danhSachLopBindingSource.Add(dslop);
                danhSachLopBindingSource1.RemoveCurrent();
            }

        }

        private void btnFrom2_Click(object sender, EventArgs e)
        {
            if (grvForm.RowCount == 0)
            {
                MessageBox.Show("Không có dữ liệu");
            }
            else
            {
                int i = 0;
                while (grvForm.RowCount != 0)
                {
                    var row = grvForm.GetRow(i) as DanhSachLop;
                    var dslop = new DanhSachLop
                    {
                        ID_sv = row.ID_sv,
                        ID_lop = row.ID_lop
                    };

                    danhSachLopBindingSource1.Add(dslop);
                    danhSachLopBindingSource.RemoveCurrent();
                }
                
            }
        }

        private void btnTo2_Click(object sender, EventArgs e)
        {
            if (grvTo.RowCount == 0)
            {
                MessageBox.Show("Không có dữ liệu");
            }
            else
            {
                int i = 0;
                while (grvTo.RowCount != 0)
                {
                    var row = grvTo.GetRow(i) as DanhSachLop;
                    var dslop = new DanhSachLop
                    {
                        ID_sv = row.ID_sv,
                        ID_lop = row.ID_lop
                    };

                    danhSachLopBindingSource.Add(dslop);
                    danhSachLopBindingSource1.RemoveCurrent();
                }
            }
        }

        private void btnChuyenLop_Click(object sender, EventArgs e)
        {
            if (grvTo.RowCount == 0)
            {
                MessageBox.Show("Không có dữ liệu");
            }
            else
            {
                Context db = new Context();
                id_lop1s = int.Parse(cmbLop1.SelectedValue.ToString());
                id_lop2s = int.Parse(cmbLop2.SelectedValue.ToString());
                string tenlop = db.Lops.Where(p => p.ID_lop == id_lop2s).Select(o=>o.Ten_lop).First();
                for (int i = 0; i < grvTo.RowCount;i++ )
                {
                    var row = grvTo.GetRow(i) as DanhSachLop;
                    var dslop = new DanhSachLop
                    {
                        ID_sv = row.ID_sv,
                        ID_lop = row.ID_lop
                    };
                    danhsachlop.Add(dslop);
                    var sv = db.HoSoSinhViens.Where(p => p.ID_sv == row.ID_sv).First();
                    sv.Lop = tenlop;
                    db.SubmitChanges();
                    var sv1 = db.DanhSachLops.Where(p => p.ID_sv == row.ID_sv).First();
                    sv1.ID_lop = id_lop2s;
                    db.SubmitChanges();
                }
                frmBaoLuu a = new frmBaoLuu();
                a.ShowDialog();
            }
        }
    }
}